package com.briup.product_source.service.impl;

import com.briup.product_source.dao.ManagerHurdlesMapper;
import com.briup.product_source.dao.ext.ManagerHurdlesExtMapper;
import com.briup.product_source.exception.ServiceException;
import com.briup.product_source.pojo.ManagerHurdles;
import com.briup.product_source.pojo.ext.ManagerHurdlesExt;
import com.briup.product_source.result.ResultCode;
import com.briup.product_source.service.HurdlesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLRecoverableException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class HurdlesServiceImpl implements HurdlesService {

    @Autowired
    private ManagerHurdlesMapper managerHurdlesMapper;

    @Autowired
    ManagerHurdlesExtMapper managerHurdlesExtMapper;

    @Override
    public PageInfo<ManagerHurdlesExt> queryAllAndPage(Integer pageNum,Integer pageSize,String hName,
                                                       Integer hMax, String fhName, String hEnable) {
        PageHelper.startPage(pageNum,pageSize);
        List<ManagerHurdlesExt> managerHurdlesList = managerHurdlesExtMapper.queryAll(hName, hMax, fhName, hEnable);
        PageInfo<ManagerHurdlesExt> pageInfo = new PageInfo<>(managerHurdlesList);
        return pageInfo;
    }

    @Override
    public List<Integer> findAllMax() {
        List<Integer> allMax = managerHurdlesMapper.findAllMax();
        return allMax;
    }

    @Override
    public void changeStatus(String hId, String hEnable) {
        if(managerHurdlesMapper.selectById(hId) != null){
            String status = "可用";
            if(status.equals(hEnable)){
                status = "禁用";
            }
            int i = managerHurdlesMapper.changeStautus(hId,status);
            if(i == 0){
                throw new ServiceException(ResultCode.FAIL);
            }
        }else {
            throw new ServiceException(ResultCode.DATA_IS_EMPTY);
        }
    }

    @Override
    public void changeStatusByChoice(List<Map<String, String>> ids) {
        int count = 0;
        for(Map<String,String> map : ids){
            String hId = map.get("hId");
            String hEnable = map.get("hEnable");
            if(managerHurdlesMapper.selectById(hId) == null){
                continue;
            }
            String status = "禁用";
            if(status.equals(hEnable)){
                status = "可用";
            }
            int i = managerHurdlesMapper.changeStautus(hId,status);
            count += i;
        }
        if(count == 0)
            throw new ServiceException(ResultCode.FAIL);
    }

    @Override
    public void insertOrUpdate(ManagerHurdles managerHurdles) {
        int count = 0;
        if(managerHurdles.getHId() != null){
            ManagerHurdles temp = managerHurdlesMapper.selectById(managerHurdles.getHId());
            if(temp == null){
                throw new ServiceException(ResultCode.DATA_IS_EMPTY);
            }else {
                count = managerHurdlesMapper.updateById(managerHurdles);
            }
        }else {
            managerHurdles.setHId(UUID.randomUUID().toString().replace("-", ""));
            managerHurdles.setHSaved(0);
            count = managerHurdlesMapper.insert(managerHurdles);
        }
        if (count == 0)
            throw new ServiceException(ResultCode.FAIL);
    }

    @Override
    public void deleteById(String hId) {
        ManagerHurdles temp = managerHurdlesMapper.selectById(hId);
        if(temp == null) {
            throw new ServiceException(ResultCode.DATA_IS_EMPTY);
        }
        else{
            if(temp.getHSaved() > 0 ){
                throw new ServiceException(ResultCode.DATA_CAN_NOT_DELETE);
            }else{
                if(managerHurdlesMapper.deleteById(hId) == 0){
                    throw new ServiceException(ResultCode.FAIL);
                }
            }
        }
    }

    @Override
    public List<ManagerHurdles> queryAllEnable() {
        List<ManagerHurdles> managerHurdlesList = managerHurdlesMapper.queryAllEnable();
        return managerHurdlesList;
    }
}

